﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using edu.Model;
using System.Data;
using Discuz.Data;
using edu.Comm;
using System.Data.Common;
using System.Data.SqlClient;

namespace edu.Service
{

    public class TeacherService
    {
        public static int Add(Teacher entity)
        {
            int id;
            string sql = string.Format("insert into [edu_teacher]([Code],[Name],[Description],[SchoolId],[UserId],[CourseId],[Score]) values(@Code,@Name,@Description,@SchoolId,@UserId,@CourseId,@Score)");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Code", Value=entity.Code},
                new SqlParameter(){DbType=DbType.String, ParameterName="Name", Value=entity.Name},
                new SqlParameter(){DbType=DbType.String, ParameterName="Description", Value=entity.Description},
                new SqlParameter(){DbType=DbType.Int32, ParameterName="SchoolId", Value=entity.SchoolId},
                new SqlParameter(){DbType=DbType.Int32, ParameterName="UserId", Value=entity.UserId},
                new SqlParameter(){DbType=DbType.Int32, ParameterName="CourseId", Value=entity.CourseId},
                new SqlParameter(){DbType=DbType.String, ParameterName="Score", Value=entity.Score}
                                };
            DbHelper.ExecuteNonQuery(out id, CommandType.Text, sql, param);
            return id;
        }

        public static void Update(Teacher entity)
        {
            string sql = string.Format("update edu_teacher set [Code]=@Code,[Name]=@Name,[Description]=@Description,[SchoolId]=@SchoolId,[UserId]=@UserId,[CourseId]=@CourseId,[Score]=@Score where Id=@Id");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Code", Value=entity.Code},
                new SqlParameter(){DbType=DbType.String, ParameterName="Name", Value=entity.Name},
                new SqlParameter(){DbType=DbType.String, ParameterName="Description", Value=entity.Description},
                new SqlParameter(){DbType=DbType.Int32, ParameterName="SchoolId", Value=entity.SchoolId},
                new SqlParameter(){DbType=DbType.Int32, ParameterName="UserId", Value=entity.UserId},
                new SqlParameter(){DbType=DbType.Int32, ParameterName="CourseId", Value=entity.CourseId},
                new SqlParameter(){DbType=DbType.String, ParameterName="Score", Value=entity.Score},
                new SqlParameter(){DbType=DbType.String, ParameterName="Id", Value=entity.Id}
                                };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static void Delete(Teacher entity)
        {
            throw new NotImplementedException();
        }

        public static void Delete(int id)
        {
            string sql = string.Format("delete from edu_teacher where Id=@Id");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Id", Value=id}
                              };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static DataTable GetTopTeacher()
        {
            string sql = "select a.name,b.name,a.score from edu_teacher a left join edu_course b on a.courseid=b.id order by Score desc";
            return DbHelper.ExecuteDataset(sql).Tables[0];
        }

        /// <summary>
        /// 注意，需要判断是否班级已经毕业
        /// 即只返回现在正在执教的班级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataTable GetClass(int id)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 取出执教班级历史记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataTable GetHistory(int id)
        {
            throw new NotImplementedException();
            
        }
    }
}
